package book_manage_system.opera;

import book_manage_system.book.Book;
import book_manage_system.book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation{ //删除
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书");
        int currentSize = bookList.getUsedSize();
        int index = -1;
        boolean b = true;
        do {
            System.out.println("请输入你要删除的书名");
            Scanner scanner = new Scanner(System.in);
            String name = scanner.nextLine();
            for (int i = 0; i < currentSize; i++) {
                Book tmp = bookList.getBooks(i);
                if (tmp.getName().equals(name)) {
                    index = i;
                    b = false;
                    break;
                }
                if(i == currentSize-1){
                    System.out.println("该书不存在,请重新输入");
                }
            }
        }while (b);
        for (int j = index; j < currentSize-1; j++) {
            Book book = bookList.getBooks(j+1);
            bookList.setBooks(j,book);
        }
        bookList.setUsedSize(currentSize-1);
        bookList.setBooks(currentSize-1,null);
        System.out.println("删除成功");
    }
}
